/*
 * TimeValueDisplay.java
 *
 * Created on 02 January 2004, 06:28
 * Copyright (C) 2004, Velocityme Partnership
 */

package com.velocityme.client.gui.time;

import com.velocityme.client.gui.calendar.CalendarDatePanel;
import com.velocityme.client.gui.calendar.CalendarTimePanel;
import com.velocityme.valueobjects.StateValue;
import com.velocityme.valueobjects.StateValueToString;
import com.velocityme.valueobjects.TimeValue;
import java.text.ParseException;
import java.util.Date;

/**
 *
 * @author  Robert Crida Work
 */
public class TimeValueDisplay extends javax.swing.JPanel {
    
    private TimeValue m_timeValue;
    private CalendarDatePanel m_datePanel;
    private CalendarTimePanel m_startTimePanel;
    private CalendarTimePanel m_stopTimePanel;

    /** Creates new form TimeValueDisplay */
    public TimeValueDisplay(String taskPathName, TimeValue p_timeValue, Byte p_percentageComplete, StateValue p_defaultStateValue, StateValue[] p_allStateValues) {
        initComponents();
        Date today = new Date();
        jTextFieldTask.setText(taskPathName);
        
        m_datePanel = new CalendarDatePanel();
        m_startTimePanel = new CalendarTimePanel();
        m_stopTimePanel = new CalendarTimePanel();
        java.awt.GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridwidth = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        add(m_datePanel, gridBagConstraints);
        gridBagConstraints.gridx = 3;
        add(m_startTimePanel, gridBagConstraints);
        gridBagConstraints.gridx = 5;
        add(m_stopTimePanel, gridBagConstraints);

        if (p_timeValue.descriptionHasBeenSet()) {
            jTextAreaDescription.setText(p_timeValue.getDescription());
        }
        jSliderPercentageComplete.setValue(p_percentageComplete.intValue());
        jComboBoxState.removeAllItems();
        for (int i = 0; i < p_allStateValues.length; i++)
            jComboBoxState.addItem(new StateValueToString(p_allStateValues[i]));
        if (p_timeValue.getStateValue() != null)
            jComboBoxState.setSelectedItem(new StateValueToString(p_timeValue.getStateValue()));
        else
            jComboBoxState.setSelectedItem(new StateValueToString(p_defaultStateValue));
        m_datePanel.setDate(p_timeValue.startTimeHasBeenSet()? p_timeValue.getStartTime(): today);
        m_startTimePanel.setDate(p_timeValue.startTimeHasBeenSet()? p_timeValue.getStartTime(): today);
        m_stopTimePanel.setDate(p_timeValue.stopTimeHasBeenSet()? p_timeValue.getStopTime(): today);
        m_timeValue = p_timeValue;
    }
    
    public Byte getPercentageComplete() {
        return new Byte((byte)jSliderPercentageComplete.getValue());
    }
    
    public TimeValue getTimeValue() throws ParseException {
        m_timeValue.setDescription(jTextAreaDescription.getText());
        m_timeValue.setStateValue((StateValue)jComboBoxState.getSelectedItem());
        Date date = m_datePanel.getDate();
        Date startTime = m_startTimePanel.getDate();
        Date stopTime = m_stopTimePanel.getDate();
        date.setHours(startTime.getHours());
        date.setMinutes(startTime.getMinutes());
        m_timeValue.setStartTime(date);
        date = (Date)date.clone();
        date.setHours(stopTime.getHours());
        date.setMinutes(stopTime.getMinutes());
        m_timeValue.setStopTime(date);
        return m_timeValue;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        jLabelTask = new javax.swing.JLabel();
        jTextFieldTask = new javax.swing.JTextField();
        jLabelDescription = new javax.swing.JLabel();
        jScrollPaneDescription = new javax.swing.JScrollPane();
        jTextAreaDescription = new javax.swing.JTextArea();
        jLabelPercentageComplete = new javax.swing.JLabel();
        jSliderPercentageComplete = new javax.swing.JSlider();
        jLabelState = new javax.swing.JLabel();
        jComboBoxState = new javax.swing.JComboBox();
        jLabelDate = new javax.swing.JLabel();
        jLabelStart = new javax.swing.JLabel();
        jLabelStop = new javax.swing.JLabel();

        setLayout(new java.awt.GridBagLayout());

        jLabelTask.setText("Task:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabelTask, gridBagConstraints);

        jTextFieldTask.setEditable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.weightx = 1.0;
        add(jTextFieldTask, gridBagConstraints);

        jLabelDescription.setText("Description:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabelDescription, gridBagConstraints);

        jTextAreaDescription.setLineWrap(true);
        jTextAreaDescription.setWrapStyleWord(true);
        jScrollPaneDescription.setViewportView(jTextAreaDescription);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.weighty = 1.0;
        add(jScrollPaneDescription, gridBagConstraints);

        jLabelPercentageComplete.setText("% Complete:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabelPercentageComplete, gridBagConstraints);

        jSliderPercentageComplete.setMajorTickSpacing(10);
        jSliderPercentageComplete.setPaintLabels(true);
        jSliderPercentageComplete.setPaintTicks(true);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        add(jSliderPercentageComplete, gridBagConstraints);

        jLabelState.setText("State:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabelState, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridwidth = 6;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(jComboBoxState, gridBagConstraints);

        jLabelDate.setText("Date:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabelDate, gridBagConstraints);

        jLabelStart.setText("Start:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        add(jLabelStart, gridBagConstraints);

        jLabelStop.setText("Stop:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabelStop, gridBagConstraints);

    }//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox jComboBoxState;
    private javax.swing.JLabel jLabelDate;
    private javax.swing.JLabel jLabelDescription;
    private javax.swing.JLabel jLabelPercentageComplete;
    private javax.swing.JLabel jLabelStart;
    private javax.swing.JLabel jLabelState;
    private javax.swing.JLabel jLabelStop;
    private javax.swing.JLabel jLabelTask;
    private javax.swing.JScrollPane jScrollPaneDescription;
    private javax.swing.JSlider jSliderPercentageComplete;
    private javax.swing.JTextArea jTextAreaDescription;
    private javax.swing.JTextField jTextFieldTask;
    // End of variables declaration//GEN-END:variables
    
}
